package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.utils.AliOssUtil;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理")
public class DishController {

    @Autowired
    DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;



    public void cleanCache(Long categoryId){
//       * 不会识别为 匹配式 ，需要传具体的key

        if (categoryId==null){
            //修改和批量删除
           Set keys = redisTemplate.keys("dish_*");
            Long delete = redisTemplate.delete(keys);
            log.info("删除所有分类的缓存数据:{}",delete);
        }else {
//            添加
            Boolean delete = redisTemplate.delete("dish_" + categoryId);
            log.info("删除{}分类的缓存数据",categoryId);
        }

    }


    @PostMapping
    @ApiOperation("新增菜品")
    public Result addDish(@RequestBody DishDTO dishDTO){
        log.info("菜品信息：{}",dishDTO.toString());

        dishService.addDish(dishDTO);
        cleanCache(dishDTO.getCategoryId());
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("菜品分页条件查询")
    public Result<PageResult> pageByCondition(DishPageQueryDTO dqd){

        log.info("分页查询参数：{}",dqd.toString());


        PageResult pageResult=   dishService.pageByCondition(dqd);



        return Result.success(pageResult);



    }


    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result deleteByIds(@RequestParam List<Long> ids){



        log.info("菜品ids：{}",ids);

        dishService.deleteByIds(ids);
//        清楚所有的key
        cleanCache(null);
        return  Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询菜品")
    public Result getById(@PathVariable Long id){
        log.info("菜品id：{}",id);

        DishVO dishVO= dishService.getDishWithFlavorByDishId(id);

        return Result.success(dishVO);



    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("菜品信息：{}",dishDTO.toString());

        dishService.updateDishWithFlavor(dishDTO);
        cleanCache(null);
        return Result.success();
    }

    /**
     *根据Id查询菜品
     */
    @GetMapping("/list")
    @ApiOperation("根据id查询菜品")
    public Result<List<DishVO>> getByIdDish(Long categoryId,String name){
        log.info("根据分类id查询菜品{}{}",categoryId,name);

        Dish dish = Dish.builder().categoryId(categoryId).name(name).build();

        List<DishVO> dishVOS =dishService.getByIdDish(dish);
        return Result.success(dishVOS);
    }

}
